<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<html>
<head>
    <title>Title</title>
    <meta charset="UTF-8">
    <style>
        * {
            text-align: center;
        }

        a {
            text-decoration: none;
        }

        a:link {
            color: #646464;
        }

        a:visited {
            color: #646464;
        }

        a:visited {
            color: #646464;
        }

        a:hover {
            color: #f60;
        }

        .list-view {
            width: 800px;
            height: 450px;
            margin: 50px auto;
            border: 1px solid #000;
        }

        .header {
            width: 100%;
            height: 50px;
        }

        .header-left {
            float: left;
            padding: 5px;
        }

        .header-right {
            float: right;
            padding: 5px;
        }

        .main {
            width: 100%;
            height: 290px;
        }

        table, th, td {
            width: 800px;
            height: 50px;
            border: 1px solid #ccc;
            border-collapse: collapse;
            margin: 0 auto;
            text-align: center;
        }

        .footer {
            width: 100%;
            height: 50px;
        }

        .footer-left {
            padding-left: 20px;
            float: left;
        }

        .footer-right {
            float: right;
            padding-right: 200px;
        }
    </style>
    <script src="js/jquery.min.js"></script>
</head>
<body>
<div class="list-view">
    <h3>展示页面</h3>
    <div class="header">
        <div>
            <div class="header-left">
                <span>欢迎你，${student.name}</span>&nbsp&nbsp&nbsp&nbsp<a href="javascript:location.href='exitServlet';">重新登录</a>
            </div>
            <div class="header-right">
                <button id="btnAdd">增加</button>
                <button id="btnEdit">编辑</button>
                <button id="btnDel">删除</button>
            </div>
        </div>
    </div>

    <div class="main">
        <form action="${base}/deleteStudentServlet" method="post" id="form">
            <table>
                <tr>
                    <th><input type="checkbox" id="topCb"></th>
                    <th>姓名</th>
                    <th>学号</th>
                    <th>性别</th>
                    <th>出生日期</th>
                    <th>政治面貌</th>
                </tr>
                <c:forEach items="${students}" var="s" varStatus="status">
                    <tr>
                        <td><input type="checkbox" name="sid" value="${s.id}"></td>
                        <td><a href="${base}/detailStudentServlet?sid=${s.id}">${s.name}</a></td>
                        <td>${s.sno}</td>
                        <td>
                            <c:if test="${s.sex==1}">男</c:if>
                            <c:if test="${s.sex==0}">女</c:if>
                        </td>
                        <td><fmt:formatDate value="${s.birthday}" pattern="yyyy-MM-dd"/></td>
                        <td>
                            <c:if test="${s.polity==1}">党员</c:if>
                            <c:if test="${s.polity==2}">团员</c:if>
                            <c:if test="${s.polity==3}">无党派</c:if>
                            <c:if test="${s.polity==4}">其它</c:if>
                        </td>
                    </tr>
                </c:forEach>
            </table>
        </form>
    </div>


    <div class="footer">
        <div class="footer-left">
            <a href="${base}/listStudentServlet?pageNo=1">首页</a>
            <c:if test="${pageNo>1}">
                <a href="${base}/listStudentServlet?pageNo=${pageNo-1}">上一页</a>
            </c:if>
            <c:if test="${pageNo<pageSize}">
                <a href="${base}/listStudentServlet?pageNo=${pageNo+1}">下一页</a>
            </c:if>
            <a href="${base}/listStudentServlet?pageNo=${pageSize}">尾页</a>
        </div>
        <div class="footer-right">
            <span>共${counts}项</span>&nbsp;&nbsp;
            <span>${pageSize}页</span>
        </div>
    </div>
</div>
<script>
    $(function () {
        $("#btnAdd").click(function () {
            window.location.href = "${base}/add.jsp";
        });

        $("#btnEdit").click(function () {
            let count = 0;
            let sid = 0;
            //判断是否有选中条目
            let cbs = $("input[name='sid']");
            for (let i = 0; i < cbs.length; i++) {
                if (cbs[i].checked) {
                    count++;
                    sid = cbs[i].getAttribute("value"); //得到id
                }
            }
            if (count == 0) {
                alert("请选择要修改的学生！");
                return;
            } else if (count == 1) {
                window.location.href = "${base}/ToEditStudentServlet?sid=" + sid;
            } else {
                alert("请仅选择一个要修改的学生！");
                return;
            }


        });

        //删除事件
        $("#btnDel").click(function () {
            let flag = false;
            //判断是否有选中条目
            let cbs = $("input[name='sid']");
            for (let i = 0; i < cbs.length; i++) {
                if (cbs[i].checked) {
                    flag = true;
                    break;
                }
            }
            if (flag) { //有条目被选中
                if (confirm("确认删除吗？")) {
                    //判断是否有选中条目
                    $("#form").submit();
                }
            } else {
                alert("请选择要删除的学生！");
                return;
            }
        });

        //checkBox事件
        $("#topCb").click(function () {
            let cbs = $("input[name='sid']");
            for (let i = 0; i < cbs.length; i++) {
                cbs[i].checked = this.checked;
            }
        });


    });
</script>
</body>
</html>